<template>
  <div class="admin-login">
    <div class="login-container">
      <div class="login-card">
        <!-- 头部 -->
        <div class="login-header">
          <div class="logo">
            <div class="login-logo">🏫</div>
            <h1>浙江高职招生管理系统</h1>
          </div>
          <p>管理员登录</p>
        </div>

        <!-- 登录表单 -->
        <div class="login-form">
          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginRules"
            size="large"
          >
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                placeholder="请输入用户名"
                prefix-icon="User"
                clearable
              />
            </el-form-item>
            
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
                prefix-icon="Lock"
                clearable
                show-password
                @keyup.enter="handleLogin"
              />
            </el-form-item>
            
            <el-form-item>
              <el-button
                type="primary"
                size="large"
                style="width: 100%"
                :loading="loading"
                @click="handleLogin"
              >
                {{ loading ? '登录中...' : '登录' }}
              </el-button>
            </el-form-item>
          </el-form>
        </div>

        <!-- 底部信息 -->
        <div class="login-footer">
          <p>默认账号：admin / admin123</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { useAdminStore } from '@/stores/admin'
import { ElMessage } from 'element-plus'

const router = useRouter()
const adminStore = useAdminStore()

// 登录表单
const loginFormRef = ref()
const loginForm = reactive({
  username: 'admin',
  password: 'admin123'
})

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

// 加载状态
const loading = ref(false)

// 处理登录
const handleLogin = async () => {
  try {
    // 表单验证
    await loginFormRef.value.validate()
    
    loading.value = true
    
    // 执行登录
    await adminStore.adminLogin(loginForm)
    
    // 登录成功，跳转到管理首页
    router.push('/admin/dashboard')
    
  } catch (error) {
    console.error('登录失败:', error)
  } finally {
    loading.value = false
  }
}

// 检查是否已登录
onMounted(() => {
  if (adminStore.isLoggedIn) {
    router.push('/admin/dashboard')
  }
})
</script>

<style lang="scss" scoped>
.admin-login {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  
  .login-container {
    width: 100%;
    max-width: 400px;
    
    .login-card {
      background: white;
      border-radius: 16px;
      padding: 40px;
      box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
      backdrop-filter: blur(10px);
      
      .login-header {
        text-align: center;
        margin-bottom: 40px;
        
        .logo {
          display: flex;
          flex-direction: column;
          align-items: center;
          margin-bottom: 16px;
          
          img {
            width: 64px;
            height: 64px;
            margin-bottom: 12px;
          }
          
          h1 {
            font-size: 20px;
            color: #333;
            margin: 0;
          }
        }
        
        p {
          color: #666;
          font-size: 14px;
          margin: 0;
        }
      }
      
      .login-form {
        .el-form-item {
          margin-bottom: 24px;
          
          &:last-child {
            margin-bottom: 0;
          }
        }
      }
      
      .login-footer {
        text-align: center;
        margin-top: 24px;
        
        p {
          color: #999;
          font-size: 12px;
          margin: 0;
        }
      }
    }
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .admin-login {
    padding: 10px;
    
    .login-card {
      padding: 30px 20px;
      
      .login-header {
        margin-bottom: 30px;
        
        .logo {
          h1 {
            font-size: 18px;
          }
        }
      }
    }
  }
}
</style>